package com.ruoyi.course.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.course.service.IOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/system/oss")
public class OssController {
    @Autowired
    private IOssService ossService;

    /**
     * OSS文件上传（支持图片和普通文件，限制1024MB）
     */
    @PreAuthorize("@ss.hasPermi('system:oss:upload')")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) {
        try {
            String url = ossService.uploadFile(file);
            return AjaxResult.success().put("url", url).put("fileName", file.getOriginalFilename());
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}